package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealServiceMapper;
import com.sky.mapper.UserShopCarServiceMapper;
import com.sky.service.UserShopCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.constructor.BaseConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 符健清
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Slf4j
@Service
public class UserShopServiceCarimpl implements UserShopCarService {
 @Autowired
  private SetMealServiceMapper setMealServiceMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private UserShopCarServiceMapper userShopCarServiceMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void insertShoppingCar(ShoppingCartDTO shoppingCartDTO) {
//         判定当前添加的菜品是否存在
//        通过userid  dishID  flavor  setmeal id  查询 菜单
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("shoppingCart:{}",shoppingCart);
        List <ShoppingCart> list=userShopCarServiceMapper.selectShoppingBy(shoppingCart);
        log.info("list:{}" ,list);
        if(list !=null && list.size()>0) {
            ShoppingCart  cart = list.get(0);
//           菜品数量加1
//        判定添加的类型是否菜单类型 /套餐类型
            if (shoppingCartDTO.getDishId() != null) {
                shoppingCart.setCreateTime(LocalDateTime.now());
            } else {
//              套餐数量加1
                    shoppingCart.setCreateTime(LocalDateTime.now());
            }
            shoppingCart.setNumber(cart.getNumber()+1);
            userShopCarServiceMapper.updateShoppingCartNumber(shoppingCart);
        }
//       如何查讯到的list  is null 则添加新的数据
        else {
            ShoppingCart  shoppingCarts = new ShoppingCart();
//            判定添加的是菜品还是套餐系类
            if (shoppingCartDTO.getDishId() != null) {
//                添加菜品
//                获取菜品信息
                List<Dish> dishList = dishMapper.selectDishById(shoppingCartDTO.getDishId());
                Dish dish = dishList.get(0);
//              修改其他属性
                shoppingCarts.setName(dish.getName());
                shoppingCarts.setUserId(BaseContext.getCurrentId());
                shoppingCarts.setDishId(dish.getId());
                shoppingCarts.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCarts.setAmount(dish.getPrice());
                shoppingCarts.setImage(dish.getImage());
                shoppingCarts.setCreateTime(LocalDateTime.now());
            }
            else {
//               添加套餐
//                获取套餐信息
                Setmeal setmeal = setMealServiceMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
//              修改其他属性
                shoppingCarts.setName(setmeal.getName());
                shoppingCarts.setUserId(BaseContext.getCurrentId());
                shoppingCarts.setSetmealId(setmeal.getId());
                shoppingCarts.setAmount(setmeal.getPrice());
                shoppingCarts.setImage(setmeal.getImage());
                shoppingCarts.setCreateTime(LocalDateTime.now());
            }
            userShopCarServiceMapper.insertShoppingCart(shoppingCarts);
        }
    }

    /**
     *查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> selectShppingCart() {
        Long userId  = BaseContext.getCurrentId();
        List<ShoppingCart> list =userShopCarServiceMapper.selectShopping(userId);
        return  list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCarts() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        userShopCarServiceMapper.deleteAllShoppingCar(shoppingCart);

    }

    /**
     * 购物车数量 减1
     * @param shoppingCartDTO
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
//        查询当前 线程用户的userid   dish_ id  dish_flavor  semeal_id 是否 有数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("shoppingCart:{}",shoppingCart);
        List <ShoppingCart> list=userShopCarServiceMapper.selectShoppingBy(shoppingCart);
        ShoppingCart   cart = list.get(0);
        log.info("cart.getNumber :{} ",cart.getNumber());
        if(cart.getNumber() >=2){
            if (cart.getNumber() != null && cart.getNumber() > 0) {
                //        菜品  数量减一
//        判定添加的类型是否菜单类型 /套餐类型
                if (shoppingCartDTO.getDishId() != null) {
                    shoppingCart.setCreateTime(LocalDateTime.now());
                } else {
//         套餐 数量 减一
                    shoppingCart.setCreateTime(LocalDateTime.now());
                }
                shoppingCart.setNumber(cart.getNumber() - 1);
                userShopCarServiceMapper.updateShoppingCartNumber(shoppingCart);
            }
        }else{
            userShopCarServiceMapper.deleteShoppingCartOne(shoppingCart);
        }

    }


}
